#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Main entry point for the Cursor Register GUI application
"""

import sys
import threading
import traceback
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer
from modules.gui.main_window import CursorRegisterGUI

def excepthook(exc_type, exc_value, exc_traceback):
    """
    Global exception handler to catch unhandled exceptions
    """
    error_msg = ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))
    print(f"Unhandled exception: {error_msg}")
    # In a real app, you might want to log to a file or show a dialog

def create_worker_thread(target_func, name=None):
    """Create a worker thread for background tasks"""
    worker_thread = threading.Thread(target=target_func, name=name)
    worker_thread.daemon = True  # Make thread exit when main thread exits
    return worker_thread

def main():
    """
    Main function to start the application
    """
    # Set global exception handler
    sys.excepthook = excepthook
    
    # Initialize QApplication in the main thread
    app = QApplication(sys.argv)
    
    # Create main window
    window = CursorRegisterGUI()
    window.show()
    
    # Set up a timer to keep UI responsive
    # This helps process events and keeps UI responsive
    refresh_timer = QTimer()
    refresh_timer.timeout.connect(lambda: None)  # Empty callback
    refresh_timer.start(100)  # 100ms interval
    
    # Start the application event loop
    sys.exit(app.exec_())

if __name__ == "__main__":
    main() 